### Launching all pair spread computations
### Trying to forecast the spread between ?/? bonds futures
# library("SIT")
library("RPQuantUtils")
## Loading required package: waveslim
## 
## waveslim: Wavelet Method for 1/2/3D Signals (version = 1.7.5)
## 
## Loading required package: sendmailR
library("RPToolsDB")
## Loading required package: RPostgreSQL
## Loading required package: DBI
## Loading required package: lubridate
## 
## Attaching package: 'lubridate'
## 
## The following object is masked from 'package:waveslim':
## 
##     pm
library("RPBackTesting")
## Loading required package: PerformanceAnalytics
## Loading required package: xts
## Warning: package 'xts' was built under R version 3.2.3
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 3.2.3
## 
## Attaching package: 'zoo'
## 
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## 
## Attaching package: 'PerformanceAnalytics'
## 
## The following object is masked from 'package:graphics':
## 
##     legend
require(ggplot2)
## Loading required package: ggplot2
require(scales)
## Loading required package: scales
require(grid)
## Loading required package: grid
require("ppcor")
## Loading required package: ppcor
require(graphics)
require("TTR")
## Loading required package: TTR
require(plyr)
## Loading required package: plyr
## 
## Attaching package: 'plyr'
## 
## The following object is masked from 'package:lubridate':
## 
##     here
# require(reshape)
require(reshape2)
## Loading required package: reshape2
require(RColorBrewer)
## Loading required package: RColorBrewer
require(stats)
require(Rsolnp)
## Loading required package: Rsolnp
## Loading required package: truncnorm
## Loading required package: parallel
require(zoo)
require(xts)
require(vars)
## Loading required package: vars
## Loading required package: MASS
## Loading required package: strucchange
## Loading required package: sandwich
## Loading required package: urca
## Loading required package: lmtest
# require(Quandl)
require(rpart)
## Loading required package: rpart
require(randomForest)
## Loading required package: randomForest
## randomForest 4.6-12
## Type rfNews() to see new features/changes/bug fixes.
# require(rpart.plot)
# require(rattle)
# install.packages(pkgs = "caret", dependencies = c("Depends", "Imports"))
# require(caret)
require(xgboost)
## Loading required package: xgboost
library(caret)
## Loading required package: lattice
library(ROCR)
## Warning: package 'ROCR' was built under R version 3.2.3
## Loading required package: gplots
## Warning: package 'gplots' was built under R version 3.2.3
## 
## Attaching package: 'gplots'
## 
## The following object is masked from 'package:PerformanceAnalytics':
## 
##     textplot
## 
## The following object is masked from 'package:stats':
## 
##     lowess
# source("E:/research/Projects/sduprey/NAR-271/RCode/RP_Plotting_Utils.R")
# source("E:/research/Projects/sduprey/NAR-271/RCode/RP_Macro_Monthly_Utils.R")
# source("E:/research/Projects/sduprey/NAR-271/RCode/RP_Spread_Utils.R")

user = 'sduprey'
# JIRA Code (e.g. NAR-#)
JIRACode = 'NAR-271'
repoPath = RP_GetSharedPath(user)
# Input Data Path
inputDataPath  = paste(repoPath,'InputData/', user,'/',JIRACode,'/',sep="")
# Output Data Path
outputDataPath = paste(repoPath,'OutputData/', user,'/',JIRACode,'/',sep="")

outputDataPathMonth <- paste(outputDataPath,"Month_2007/01_11_2015/",sep="")
outputDataPathStrategyMonth <- paste(outputDataPath,"Month_2007/01_11_2015/",sep="")
# outputDataPathMonth <- paste(outputDataPath,"Month_2007/",sep="")
# outputDataPathStrategyMonth <- paste(outputDataPath,"Month_2007/",sep="")

results <- readRDS(paste(outputDataPathStrategyMonth,"all_pairs_results_month_2007.rds", sep = ""))

Individual pair US/Germany trading strategy

### Deutschland visualization
my_result_spread_name <- "US_DE"
my_pair <- c("US","DE")
my_pair[1]<-"US"
my_pair[2]<-"DE"
# Long/short position returns distribution
long_US <- results$DE_FIRST_WEIGHT>=0
long_DE <- results$DE_SECOND_WEIGHT>=0
LONG_STRATEGY_RETURN_US <- results$DE_FIRST_WEIGHT[long_US]*results$DE_FIRST_BOND_NEXT_RETURN[long_US]
LONG_STRATEGY_RETURN_DE <- results$DE_SECOND_WEIGHT[long_DE]*results$DE_SECOND_BOND_NEXT_OPEN_RETURN[long_DE]


print("Mean return for long position both US and DE")
## [1] "Mean return for long position both US and DE"
mean((LONG_STRATEGY_RETURN_US+LONG_STRATEGY_RETURN_DE))
## Warning in LONG_STRATEGY_RETURN_US + LONG_STRATEGY_RETURN_DE: longer object
## length is not a multiple of shorter object length
## [1] 0.006325332
print("Mean return for long position for US only")
## [1] "Mean return for long position for US only"
mean(LONG_STRATEGY_RETURN_US)
## [1] 0.003372029
print("Mean return for long position for DE only")
## [1] "Mean return for long position for DE only"
mean(LONG_STRATEGY_RETURN_DE)
## [1] 0.003141362
# Short position returns distribution
short_US <- results$DE_FIRST_WEIGHT<=0
short_DE <- results$DE_SECOND_WEIGHT<=0
SHORT_STRATEGY_RETURN_US <- results$DE_FIRST_WEIGHT[short_US]*results$DE_FIRST_BOND_NEXT_RETURN[short_US]
SHORT_STRATEGY_RETURN_DE <- results$DE_SECOND_WEIGHT[short_DE]*results$DE_SECOND_BOND_NEXT_OPEN_RETURN[short_DE]
print("Mean return for short position both US and DE")
## [1] "Mean return for short position both US and DE"
mean((SHORT_STRATEGY_RETURN_US+SHORT_STRATEGY_RETURN_DE))
## Warning in SHORT_STRATEGY_RETURN_US + SHORT_STRATEGY_RETURN_DE: longer
## object length is not a multiple of shorter object length
## [1] -0.0001433032
print("Mean return for long position for US only")
## [1] "Mean return for long position for US only"
mean(SHORT_STRATEGY_RETURN_US)
## [1] 0.0004396305
print("Mean return for long position for DE only")
## [1] "Mean return for long position for DE only"
mean(SHORT_STRATEGY_RETURN_DE)
## [1] -0.0005169291
### Nice visualization of superposed densities
return_decomposition_df <- data.frame(returns=LONG_STRATEGY_RETURN_US,typo="LONG_US")
return_decomposition_df <- rbind(return_decomposition_df, data.frame(returns=LONG_STRATEGY_RETURN_DE,typo="LONG_DE"))
return_decomposition_df <- rbind(return_decomposition_df, data.frame(returns=SHORT_STRATEGY_RETURN_US,typo="SHORT_US"))
return_decomposition_df <- rbind(return_decomposition_df, data.frame(returns=SHORT_STRATEGY_RETURN_DE,typo="SHORT_DE"))

my_title <-"Distribution of returns per trade type"
my_xaxis_title <- paste("Returns")
my_yaxis_title <- paste("Percentage","\n")

g <- ggplot(return_decomposition_df, aes(x=returns, fill=typo)) +geom_density(alpha=.3)+
  scale_y_continuous(labels = percent_format())+scale_x_continuous(limits=c(-0.1,0.1))+facet_wrap(~typo, ncol = 2)
g <- g +ylab(my_yaxis_title)+xlab(my_xaxis_title)+
  theme(axis.text.x = element_text(size=12),axis.text.y = element_text(size=12),title =element_text(size=18, face='bold'))+
  theme(legend.position=c(0.9,0.85), legend.box = "vertical")+
  theme(legend.background = element_rect(fill="gray90"))+
  theme(legend.key.size = unit(1., "cm"))+
  theme(legend.text =  element_text(size=12,colour="black"))+
  theme(legend.title =  element_text(size=12,colour="black"))
print(g)

g <- ggplot(return_decomposition_df, aes(x=returns, fill=typo)) +geom_density(alpha=.3)+
  scale_y_continuous(labels = percent_format())+scale_x_continuous(limits=c(-0.1,0.1))
g <- g +ylab(my_yaxis_title)+xlab(my_xaxis_title)+
  theme(axis.text.x = element_text(size=12),axis.text.y = element_text(size=12),title =element_text(size=18, face='bold'))+
  theme(legend.position=c(0.9,0.85), legend.box = "vertical")+
  theme(legend.background = element_rect(fill="gray90"))+
  theme(legend.key.size = unit(1., "cm"))+
  theme(legend.text =  element_text(size=12,colour="black"))+
  theme(legend.title =  element_text(size=12,colour="black"))
print(g)

print("Outputing statistical results for our individual trading strategy US_DE")
## [1] "Outputing statistical results for our individual trading strategy US_DE"
WeightMatrix <- results[,c("DATES","DE_FIRST_WEIGHT","DE_SECOND_WEIGHT")]
colnames(WeightMatrix) <- c("DATE","DE_FIRST_WEIGHT","DE_SECOND_WEIGHT")
ReturnSerie <- results[,c("DE_STRATEGY_RETURN")]
turnover <- RP_GetTurnOver(WeightMatrix,-1)
RP_ReturnStats(ReturnSerie,21,TRUE,WeightMatrix,0.05,F)
## Warning in if (is.na(WeightMatrix)) {: the condition has length > 1 and
## only the first element will be used
## *********************************************************
## **********       STRATEGY STATISTICS           **********
## *********************************************************
## *****                NO FEE                         *****
## ****                                                 ****
##   Annualized Return:      0.0405744924781351 
##   Annualized Volatility:  0.0570704226212263 
##   Information Ratio:     0.710954827642089 
##   Hit Ratio:             0.526315789473684 
##   W/L Ratio:             1.56290398966559 
##   Turnover:              0.684768579357596 
##   P-Value:               0.0483439829857331 
##   Max Drawdown:          -0.0649320591159946 
##   Drawdown Recovery:     12 
##   Break-Even Fee (bps):  24.7304749333123 
## ****                                                 ****
## *********************************************************
## *****                WITH FEE                       *****
## ****                                                 ****
##   Annualized Return:      0.032374894233945 
##   Annualized Volatility:  0.057216801104164 
##   Information Ratio:     0.565828456138365 
##   Hit Ratio:             0.515789473684211 
##   W/L Ratio:             1.45230787780919 
##   Turnover:              0.684768579357596 
##   P-Value:               0.114731515511712 
##   Max Drawdown:          -0.0712019361640389 
##   Drawdown Recovery:     20 
## ****                                                 ****
## *********************************************************
## $STATS
##   Annualized_Return Annualized_Return_Fee Annualized_Volatility
## 1        0.04057449            0.03237489            0.05707042
##   Annualized_Volatility_Fee        IR    IR_Fee  TurnOver  HitRatio
## 1                 0.0572168 0.7109548 0.5658285 0.6847686 0.5263158
##   HitRatio_Fee  WLRatio WLRatio_Fee       PVal  PVal_Fee BreakEvenFee
## 1    0.5157895 1.562904    1.452308 0.04834398 0.1147315     24.73047
##   maxDrawdown drawdownRecovery maxDrawdown_Fee drawdownRecovery_Fee
## 1 -0.06493206               12     -0.07120194                   20
## 
## $RETURN
##  [1]  1.386014e-02 -7.866707e-03  6.671047e-03 -5.549961e-05  1.448478e-02
##  [6] -2.386575e-03  5.659555e-03 -1.051093e-02 -1.276292e-02 -5.206617e-03
## [11]  2.030408e-02 -2.557707e-03  1.346095e-02 -2.484791e-02  1.860710e-02
## [16] -1.185510e-02  2.020557e-02 -1.613924e-02  5.111759e-04  8.622527e-02
## [21]  5.121851e-03 -6.802425e-03 -2.046197e-03  2.001307e-03 -2.198229e-02
## [26] -1.940453e-02 -1.504567e-03 -3.819390e-03  1.006365e-02  6.650989e-03
## [31]  1.153533e-02  2.313315e-02 -1.810004e-02 -1.000794e-02 -7.473154e-03
## [36]  1.222698e-02  2.732562e-02 -1.444506e-02 -7.203586e-04  2.382755e-02
## [41] -3.327787e-03  3.369918e-03  2.021990e-02  3.764966e-03 -9.100876e-04
## [46] -1.299340e-02  1.624268e-02  2.411356e-02  9.119496e-03  3.190896e-02
## [51]  1.889871e-02  1.490993e-02 -3.292237e-03  3.124314e-05 -1.133851e-02
## [56]  7.377932e-03  8.195109e-03  2.983287e-03  2.482575e-02  1.987739e-02
## [61]  8.674019e-03 -1.287425e-02 -6.666747e-04 -7.963122e-04  1.354306e-02
## [66]  8.804724e-03  1.780176e-02  1.546758e-02 -1.465697e-03 -3.211604e-02
## [71] -4.753048e-03 -2.022400e-02  1.422615e-02 -7.343732e-03  3.694486e-03
## [76] -1.810737e-02  2.438462e-02 -3.125737e-03  1.410193e-02 -1.121979e-02
## [81]  1.007843e-02 -5.003322e-03  4.517377e-03 -1.510199e-03  4.665145e-03
## [86] -1.163251e-02  2.470894e-02  2.986021e-02 -1.794295e-02 -1.254946e-02
## [91]  1.783127e-02  1.724134e-02 -1.925067e-02 -1.074578e-02 -1.241651e-02
## 
## $RETURN_FEE
##  [1]  0.0133668984 -0.0082405425  0.0056772026 -0.0009726376  0.0144847827
##  [6] -0.0032222483  0.0056595546 -0.0106582548 -0.0129105723 -0.0061284935
## [11]  0.0196833975 -0.0035607705  0.0133245643 -0.0257880795  0.0177887953
## [16] -0.0126680038  0.0192413710 -0.0162759991  0.0001527845  0.0858356783
## [21]  0.0045914733 -0.0072393670 -0.0021010222  0.0012248228 -0.0223818546
## [26] -0.0204246473 -0.0020487295 -0.0047399884  0.0100636505  0.0058228372
## [31]  0.0106287611  0.0221555413 -0.0183840271 -0.0109976654 -0.0083971238
## [36]  0.0119261248  0.0273075135 -0.0154601220 -0.0017215802  0.0236599121
## [41] -0.0042479328  0.0023727857  0.0193211747  0.0029696913 -0.0018280101
## [46] -0.0135742663  0.0158191987  0.0239371309  0.0081280824  0.0310206841
## [51]  0.0179169537  0.0143857073 -0.0033396056 -0.0008858153 -0.0116903852
## [56]  0.0063847899  0.0072855103  0.0020689333  0.0247444571  0.0189783586
## [61]  0.0076821636 -0.0132480628 -0.0006666747 -0.0017976100  0.0126383190
## [66]  0.0078129980  0.0172222439  0.0145645792 -0.0024676654 -0.0330835174
## [71] -0.0056943980 -0.0212449515  0.0141295079 -0.0083516101  0.0030199392
## [76] -0.0190412231  0.0236814993 -0.0036093898  0.0131154470 -0.0118041864
## [81]  0.0090879691 -0.0059803291  0.0035213886 -0.0025122120  0.0036693036
## [86] -0.0126447251  0.0238142495  0.0288891614 -0.0187108869 -0.0134781372
## [91]  0.0172366217  0.0168245452 -0.0192966457 -0.0116727789 -0.0133450653
toplot_df <-  melt(results[,c("DATES", "DE_STRATEGY_TODAY","DE_FIRST_BOND","DE_SECOND_BOND")],"DATES")
my_title <- paste("Macro Sentiment ML over Ravenpack ESS metrics and taxonomy ",my_pair[1],"/",my_pair[2],sep="")
g<-ggplot(
  toplot_df,aes(
    x = DATES,y = value,group = variable,color = variable
  )
) +
  geom_line() +
  scale_x_date() +
  ggtitle(my_title) + xlab("Time") + ylab("Cumulated sentiment") +
  theme(title = element_text(size = 12, face = 'bold')) +
  theme(legend.position = c(0.2,0.8), legend.box = "vertical") +
  theme(legend.background = element_rect(fill = "gray90")) +
  theme(legend.key.size = unit(0.7, "cm"))
print(g)

# Individual pair US/UK trading strategy

### Great Britain visualization
my_result_spread_name <- "US_GB"
my_pair[1]<-"US"
my_pair[2]<-"GB"
# Long/short position returns distribution
long_US <- results$GB_FIRST_WEIGHT>=0
long_GB <- results$GB_SECOND_WEIGHT>=0
LONG_STRATEGY_RETURN_US <- results$GB_FIRST_WEIGHT[long_US]*results$GB_FIRST_BOND_NEXT_RETURN[long_US]
LONG_STRATEGY_RETURN_GB <- results$GB_SECOND_WEIGHT[long_GB]*results$GB_SECOND_BOND_NEXT_OPEN_RETURN[long_GB]

print("Mean return for long position both US and GB")
## [1] "Mean return for long position both US and GB"
mean((LONG_STRATEGY_RETURN_US+LONG_STRATEGY_RETURN_GB))
## Warning in LONG_STRATEGY_RETURN_US + LONG_STRATEGY_RETURN_GB: longer object
## length is not a multiple of shorter object length
## [1] 0.009683681
print("Mean return for long position for US only")
## [1] "Mean return for long position for US only"
mean(LONG_STRATEGY_RETURN_US)
## [1] 0.003967097
print("Mean return for long position for GB only")
## [1] "Mean return for long position for GB only"
mean(LONG_STRATEGY_RETURN_GB)
## [1] 0.005413027
# Short position returns distribution
short_US <- results$GB_FIRST_WEIGHT<=0
short_GB <- results$GB_SECOND_WEIGHT<=0
SHORT_STRATEGY_RETURN_US <- results$GB_FIRST_WEIGHT[short_US]*results$GB_FIRST_BOND_NEXT_RETURN[short_US]
SHORT_STRATEGY_RETURN_GB <- results$GB_SECOND_WEIGHT[short_GB]*results$GB_SECOND_BOND_NEXT_OPEN_RETURN[short_GB]
print("Mean return for short position both US and GB")
## [1] "Mean return for short position both US and GB"
mean((SHORT_STRATEGY_RETURN_US+SHORT_STRATEGY_RETURN_GB))
## Warning in SHORT_STRATEGY_RETURN_US + SHORT_STRATEGY_RETURN_GB: longer
## object length is not a multiple of shorter object length
## [1] 0.0008863766
print("Mean return for long position for US only")
## [1] "Mean return for long position for US only"
mean(SHORT_STRATEGY_RETURN_US)
## [1] 0.003742032
print("Mean return for long position for GB only")
## [1] "Mean return for long position for GB only"
mean(SHORT_STRATEGY_RETURN_GB)
## [1] -0.002669511
### Nice visualization of superposed densities
return_decomposition_df <- data.frame(returns=LONG_STRATEGY_RETURN_US,typo="LONG_US")
return_decomposition_df <- rbind(return_decomposition_df, data.frame(returns=LONG_STRATEGY_RETURN_GB,typo="LONG_GB"))
return_decomposition_df <- rbind(return_decomposition_df, data.frame(returns=SHORT_STRATEGY_RETURN_US,typo="SHORT_US"))
return_decomposition_df <- rbind(return_decomposition_df, data.frame(returns=SHORT_STRATEGY_RETURN_GB,typo="SHORT_GB"))

my_title <-"Distribution of returns per trade type"
my_xaxis_title <- paste("Returns")
my_yaxis_title <- paste("Percentage","\n")

g <- ggplot(return_decomposition_df, aes(x=returns, fill=typo)) +geom_density(alpha=.3)+
  scale_y_continuous(labels = percent_format())+scale_x_continuous(limits=c(-0.1,0.1))+facet_wrap(~typo, ncol = 2)
g <- g +ylab(my_yaxis_title)+xlab(my_xaxis_title)+
  theme(axis.text.x = element_text(size=12),axis.text.y = element_text(size=12),title =element_text(size=18, face='bold'))+
  theme(legend.position=c(0.9,0.85), legend.box = "vertical")+
  theme(legend.background = element_rect(fill="gray90"))+
  theme(legend.key.size = unit(1., "cm"))+
  theme(legend.text =  element_text(size=12,colour="black"))+
  theme(legend.title =  element_text(size=12,colour="black"))
print(g)

g <- ggplot(return_decomposition_df, aes(x=returns, fill=typo)) +geom_density(alpha=.3)+
  scale_y_continuous(labels = percent_format())+scale_x_continuous(limits=c(-0.1,0.1))
g <- g +ylab(my_yaxis_title)+xlab(my_xaxis_title)+
  theme(axis.text.x = element_text(size=12),axis.text.y = element_text(size=12),title =element_text(size=18, face='bold'))+
  theme(legend.position=c(0.9,0.85), legend.box = "vertical")+
  theme(legend.background = element_rect(fill="gray90"))+
  theme(legend.key.size = unit(1., "cm"))+
  theme(legend.text =  element_text(size=12,colour="black"))+
  theme(legend.title =  element_text(size=12,colour="black"))
print(g)

print("Outputing statistical results for our individual trading strategy US_GB")
## [1] "Outputing statistical results for our individual trading strategy US_GB"
WeightMatrix <- results[,c("DATES","GB_FIRST_WEIGHT","GB_SECOND_WEIGHT")]
colnames(WeightMatrix) <- c("DATE","GB_FIRST_WEIGHT","GB_SECOND_WEIGHT")
ReturnSerie <- results[,c("GB_STRATEGY_RETURN")]
turnover <- RP_GetTurnOver(WeightMatrix,-1)
RP_ReturnStats(ReturnSerie,21,TRUE,WeightMatrix,0.05,F)
## Warning in if (is.na(WeightMatrix)) {: the condition has length > 1 and
## only the first element will be used
## *********************************************************
## **********       STRATEGY STATISTICS           **********
## *********************************************************
## *****                NO FEE                         *****
## ****                                                 ****
##   Annualized Return:      0.0691642690718423 
##   Annualized Volatility:  0.0652523474462226 
##   Information Ratio:     1.05995066505222 
##   Hit Ratio:             0.694736842105263 
##   W/L Ratio:             0.98222117862386 
##   Turnover:              0.590107170317522 
##   P-Value:               0.00364322675426475 
##   Max Drawdown:          -0.0995168020064174 
##   Drawdown Recovery:     8 
##   Break-Even Fee (bps):  48.9769604048133 
## ****                                                 ****
## *********************************************************
## *****                WITH FEE                       *****
## ****                                                 ****
##   Annualized Return:      0.0621273972556519 
##   Annualized Volatility:  0.0651835295176385 
##   Information Ratio:     0.953114961945109 
##   Hit Ratio:             0.694736842105263 
##   W/L Ratio:             0.91030955308159 
##   Turnover:              0.590107170317522 
##   P-Value:               0.00865170399837462 
##   Max Drawdown:          -0.100836596742835 
##   Drawdown Recovery:     9 
## ****                                                 ****
## *********************************************************
## $STATS
##   Annualized_Return Annualized_Return_Fee Annualized_Volatility
## 1        0.06916427             0.0621274            0.06525235
##   Annualized_Volatility_Fee       IR   IR_Fee  TurnOver  HitRatio
## 1                0.06518353 1.059951 0.953115 0.5901072 0.6947368
##   HitRatio_Fee   WLRatio WLRatio_Fee        PVal    PVal_Fee BreakEvenFee
## 1    0.6947368 0.9822212   0.9103096 0.003643227 0.008651704     48.97696
##   maxDrawdown drawdownRecovery maxDrawdown_Fee drawdownRecovery_Fee
## 1  -0.0995168                8      -0.1008366                    9
## 
## $RETURN
##  [1]  0.0173514248  0.0080241006  0.0077401970 -0.0120562593 -0.0132875097
##  [6]  0.0137346522  0.0021144967 -0.0090645695  0.0280993993  0.0025888603
## [11]  0.0183135480  0.0112939929 -0.0098659040  0.0163268384 -0.0099276187
## [16]  0.0143680631  0.0229968216 -0.0101553810 -0.0023257889 -0.0881574776
## [21]  0.0194599764  0.0155698969 -0.0120453447  0.0305811959  0.0281956605
## [26] -0.0037315767 -0.0047651605  0.0372508897  0.0105882205 -0.0134893365
## [31]  0.0164363903  0.0349566280 -0.0122468963 -0.0011852880  0.0094781098
## [36] -0.0130125550  0.0213741884  0.0284746905  0.0061566342 -0.0330015076
## [41]  0.0018263353  0.0042258989  0.0188302678  0.0204292515 -0.0072191557
## [46]  0.0030573272  0.0197905513  0.0246332138 -0.0002230378  0.0381607745
## [51]  0.0237614216  0.0052685973 -0.0076021365  0.0026618488  0.0130822446
## [56]  0.0160980753  0.0070485822  0.0116499667 -0.0145847333  0.0119051976
## [61]  0.0111891339  0.0048628947  0.0064480394 -0.0040897180  0.0064457983
## [66]  0.0120940216  0.0115429521  0.0183643025  0.0047837570  0.0288561011
## [71]  0.0047097439  0.0049671046  0.0134160983  0.0062428117 -0.0048128240
## [76] -0.0216300643  0.0325673294  0.0015672032  0.0457996085  0.0098643419
## [81]  0.0056074169 -0.0096038669  0.0141364667  0.0015744594  0.0148847373
## [86] -0.0259331201 -0.0283777711  0.0276692731 -0.0371629744  0.0251343454
## [91]  0.0016190923  0.0189162942 -0.0217830262  0.0135998797 -0.0118765718
## 
## $RETURN_FEE
##  [1]  0.0168599049  0.0070316002  0.0070784517 -0.0130689011 -0.0143013997
##  [6]  0.0128843343  0.0014490168 -0.0096406714  0.0271266346  0.0025619606
## [11]  0.0180376427  0.0112291335 -0.0100971203  0.0153425484 -0.0108201143
## [16]  0.0142558368  0.0227655697 -0.0101815998 -0.0028339813 -0.0890057636
## [21]  0.0184787670  0.0145848613 -0.0123496427  0.0296108436  0.0272229894
## [26] -0.0043504494 -0.0057704422  0.0368216072  0.0102583435 -0.0135272117
## [31]  0.0160036509  0.0339905142 -0.0122890269 -0.0013732391  0.0091627808
## [36] -0.0133247714  0.0205932816  0.0275022910  0.0052417956 -0.0339974788
## [41]  0.0008276614  0.0034733524  0.0180886360  0.0201041194 -0.0078924349
## [46]  0.0023924747  0.0188096664  0.0245129058 -0.0007426102  0.0376054829
## [51]  0.0234475693  0.0046139099 -0.0083561558  0.0016640095  0.0120947543
## [56]  0.0156324415  0.0064217826  0.0106610602 -0.0152918408  0.0115748637
## [61]  0.0101997714  0.0045389337  0.0055074920 -0.0049213587  0.0057163065
## [66]  0.0113686399  0.0113692152  0.0175368840  0.0047230029  0.0282081871
## [71]  0.0041302380  0.0043877479  0.0124751506  0.0060963526 -0.0053212470
## [76] -0.0222019475  0.0324691106  0.0014159426  0.0451369676  0.0095342260
## [81]  0.0046125139 -0.0100590184  0.0131500174  0.0013953839  0.0142782760
## [86] -0.0267417041 -0.0286196792  0.0268337508 -0.0373643255  0.0250588593
## [91]  0.0012243777  0.0184329734 -0.0218907505  0.0129420019 -0.0125845922
toplot_df <-  melt(results[,c("DATES", "GB_STRATEGY_TODAY","GB_FIRST_BOND","GB_SECOND_BOND")],"DATES")

my_title <- paste("Macro Sentiment ML over Ravenpack ESS metrics and taxonomy ",my_pair[1],"/",my_pair[2],sep="")
g<-ggplot(
  toplot_df,aes(
    x = DATES,y = value,group = variable,color = variable
  )
) +
  geom_line() +
  scale_x_date() +
  ggtitle(my_title) + xlab("Time") + ylab("Cumulated sentiment") +
  theme(title = element_text(size = 12, face = 'bold')) +
  theme(legend.position = c(0.2,0.8), legend.box = "vertical") +
  theme(legend.background = element_rect(fill = "gray90")) +
  theme(legend.key.size = unit(0.7, "cm"))
print(g)

# Individual pair US/Japan trading strategy

### Japan visualization
my_result_spread_name <- "US_JP"
my_pair[1]<-"US"
my_pair[2]<-"JP"

# Long/short position returns distribution
long_US <- results$JP_FIRST_WEIGHT>=0
long_JP <- results$JP_SECOND_WEIGHT>=0
LONG_STRATEGY_RETURN_US <- results$JP_FIRST_WEIGHT[long_US]*results$JP_FIRST_BOND_NEXT_RETURN[long_US]
LONG_STRATEGY_RETURN_JP <- results$JP_SECOND_WEIGHT[long_JP]*results$JP_SECOND_BOND_NEXT_OPEN_RETURN[long_JP]

print("Mean return for long position both US and JP")
## [1] "Mean return for long position both US and JP"
mean((LONG_STRATEGY_RETURN_US+LONG_STRATEGY_RETURN_JP))
## Warning in LONG_STRATEGY_RETURN_US + LONG_STRATEGY_RETURN_JP: longer object
## length is not a multiple of shorter object length
## [1] 0.00839213
print("Mean return for long position for US only")
## [1] "Mean return for long position for US only"
mean(LONG_STRATEGY_RETURN_US)
## [1] 0.002516693
print("Mean return for long position for JP only")
## [1] "Mean return for long position for JP only"
mean(LONG_STRATEGY_RETURN_JP)
## [1] 0.005839909
# Short position returns distribution
short_US <- results$JP_FIRST_WEIGHT<=0
short_JP <- results$JP_SECOND_WEIGHT<=0
SHORT_STRATEGY_RETURN_US <- results$JP_FIRST_WEIGHT[short_US]*results$JP_FIRST_BOND_NEXT_RETURN[short_US]
SHORT_STRATEGY_RETURN_JP <- results$JP_SECOND_WEIGHT[short_JP]*results$JP_SECOND_BOND_NEXT_OPEN_RETURN[short_JP]
print("Mean return for short position both US and JP")
## [1] "Mean return for short position both US and JP"
mean((SHORT_STRATEGY_RETURN_US+SHORT_STRATEGY_RETURN_JP))
## Warning in SHORT_STRATEGY_RETURN_US + SHORT_STRATEGY_RETURN_JP: longer
## object length is not a multiple of shorter object length
## [1] 0.005151044
print("Mean return for long position for US only")
## [1] "Mean return for long position for US only"
mean(SHORT_STRATEGY_RETURN_US)
## [1] 0.0001034898
print("Mean return for long position for JP only")
## [1] "Mean return for long position for JP only"
mean(SHORT_STRATEGY_RETURN_JP)
## [1] 0.004860662
### Nice visualization of superposed densities
return_decomposition_df <- data.frame(returns=LONG_STRATEGY_RETURN_US,typo="LONG_US")
return_decomposition_df <- rbind(return_decomposition_df, data.frame(returns=LONG_STRATEGY_RETURN_JP,typo="LONG_JP"))
return_decomposition_df <- rbind(return_decomposition_df, data.frame(returns=SHORT_STRATEGY_RETURN_US,typo="SHORT_US"))
return_decomposition_df <- rbind(return_decomposition_df, data.frame(returns=SHORT_STRATEGY_RETURN_JP,typo="SHORT_JP"))

my_title <-"Distribution of returns per trade type"
my_xaxis_title <- paste("Returns")
my_yaxis_title <- paste("Percentage","\n")

g <- ggplot(return_decomposition_df, aes(x=returns, fill=typo)) +geom_density(alpha=.3)+
  scale_y_continuous(labels = percent_format())+scale_x_continuous(limits=c(-0.1,0.1))+facet_wrap(~typo, ncol = 2)
g <- g +ylab(my_yaxis_title)+xlab(my_xaxis_title)+
  theme(axis.text.x = element_text(size=12),axis.text.y = element_text(size=12),title =element_text(size=18, face='bold'))+
  theme(legend.position=c(0.9,0.85), legend.box = "vertical")+
  theme(legend.background = element_rect(fill="gray90"))+
  theme(legend.key.size = unit(1., "cm"))+
  theme(legend.text =  element_text(size=12,colour="black"))+
  theme(legend.title =  element_text(size=12,colour="black"))
print(g)

g <- ggplot(return_decomposition_df, aes(x=returns, fill=typo)) +geom_density(alpha=.3)+
  scale_y_continuous(labels = percent_format())+scale_x_continuous(limits=c(-0.1,0.1))
g <- g +ylab(my_yaxis_title)+xlab(my_xaxis_title)+
  theme(axis.text.x = element_text(size=12),axis.text.y = element_text(size=12),title =element_text(size=18, face='bold'))+
  theme(legend.position=c(0.9,0.85), legend.box = "vertical")+
  theme(legend.background = element_rect(fill="gray90"))+
  theme(legend.key.size = unit(1., "cm"))+
  theme(legend.text =  element_text(size=12,colour="black"))+
  theme(legend.title =  element_text(size=12,colour="black"))
print(g)

print("Outputing statistical results for our individual trading strategy US_JP")
## [1] "Outputing statistical results for our individual trading strategy US_JP"
WeightMatrix <- results[,c("DATES","JP_FIRST_WEIGHT","JP_SECOND_WEIGHT")]
colnames(WeightMatrix) <- c("DATE","JP_FIRST_WEIGHT","JP_SECOND_WEIGHT")
ReturnSerie <- results[,c("JP_STRATEGY_RETURN")]
turnover <- RP_GetTurnOver(WeightMatrix,-1)
RP_ReturnStats(ReturnSerie,21,TRUE,WeightMatrix,0.05,F)
## Warning in if (is.na(WeightMatrix)) {: the condition has length > 1 and
## only the first element will be used
## *********************************************************
## **********       STRATEGY STATISTICS           **********
## *********************************************************
## *****                NO FEE                         *****
## ****                                                 ****
##   Annualized Return:      0.0752306095641507 
##   Annualized Volatility:  0.0794724865559813 
##   Information Ratio:     0.946624584486322 
##   Hit Ratio:             0.578947368421053 
##   W/L Ratio:             1.52562391416373 
##   Turnover:              0.639570952138212 
##   P-Value:               0.00909994798744826 
##   Max Drawdown:          -0.126751999850965 
##   Drawdown Recovery:     12 
##   Break-Even Fee (bps):  49.1650700950605 
## ****                                                 ****
## *********************************************************
## *****                WITH FEE                       *****
## ****                                                 ****
##   Annualized Return:      0.06760762127051 
##   Annualized Volatility:  0.0794186401404185 
##   Information Ratio:     0.851281527245673 
##   Hit Ratio:             0.568421052631579 
##   W/L Ratio:             1.47503360156119 
##   Turnover:              0.639570952138212 
##   P-Value:               0.0185930054940255 
##   Max Drawdown:          -0.13649824112883 
##   Drawdown Recovery:     13 
## ****                                                 ****
## *********************************************************
## $STATS
##   Annualized_Return Annualized_Return_Fee Annualized_Volatility
## 1        0.07523061            0.06760762            0.07947249
##   Annualized_Volatility_Fee        IR    IR_Fee TurnOver  HitRatio
## 1                0.07941864 0.9466246 0.8512815 0.639571 0.5789474
##   HitRatio_Fee  WLRatio WLRatio_Fee        PVal   PVal_Fee BreakEvenFee
## 1    0.5684211 1.525624    1.475034 0.009099948 0.01859301     49.16507
##   maxDrawdown drawdownRecovery maxDrawdown_Fee drawdownRecovery_Fee
## 1   -0.126752               12      -0.1364982                   13
## 
## $RETURN
##  [1]  0.0360905964  0.0216902049  0.0138067707  0.0066026625 -0.0219279594
##  [6]  0.0409683650  0.0113346762 -0.0056636642  0.0472367360  0.0016235644
## [11]  0.0187360784  0.0363002959  0.0067027467  0.0281601099 -0.0106045054
## [16] -0.0118509460 -0.0039449185 -0.0320659977 -0.0213562296  0.0786939468
## [21] -0.0044955620  0.0130383583  0.0294834038  0.0122139331  0.0079599767
## [26] -0.0021743149  0.0009580589 -0.0224670416  0.0282705903 -0.0080528781
## [31] -0.0190974509  0.0475202301  0.0145621291  0.0080025312 -0.0460557837
## [36]  0.0027444121  0.0264482551  0.0472920434  0.0163423345  0.0197894461
## [41] -0.0090869291  0.0260048119  0.0185032733  0.0323362226  0.0027115544
## [46] -0.0132384297  0.0378591243  0.0203945646 -0.0015183760  0.0345496248
## [51]  0.0134502361 -0.0021431846 -0.0130925939  0.0023314504 -0.0165892394
## [56] -0.0050695524  0.0581133442 -0.0096156414 -0.0179486126 -0.0118043261
## [61]  0.0105003836 -0.0019493115  0.0070299183 -0.0302175669  0.0004719164
## [66] -0.0694157597 -0.0050425087  0.0105426938 -0.0063823979  0.0039349600
## [71] -0.0002961779  0.0030502440 -0.0121473248 -0.0027132850  0.0403419320
## [76] -0.0218883242  0.0317979049  0.0028514197  0.0225904588 -0.0151682287
## [81]  0.0089622195 -0.0021075775 -0.0050726311  0.0201230362  0.0430932144
## [86]  0.0275490042  0.0146119378 -0.0271565338 -0.0216353312  0.0064537824
## [91]  0.0126535670  0.0027410259 -0.0086309624 -0.0012593080 -0.0016032570
## 
## $RETURN_FEE
##  [1]  3.560820e-02  2.075570e-02  1.376194e-02  6.499849e-03 -2.195370e-02
##  [6]  4.005172e-02  1.034546e-02 -5.677278e-03  4.628242e-02  6.701130e-04
## [11]  1.775416e-02  3.589709e-02  6.702747e-03  2.723164e-02 -1.161568e-02
## [16] -1.281734e-02 -4.949376e-03 -3.211293e-02 -2.174664e-02  7.776920e-02
## [21] -4.594570e-03  1.300723e-02  2.851199e-02  1.160590e-02  7.012551e-03
## [26] -2.174315e-03  4.942585e-05 -2.246704e-02  2.737897e-02 -8.152546e-03
## [31] -2.007087e-02  4.656618e-02  1.357610e-02  7.010009e-03 -4.658212e-02
## [36]  2.248164e-03  2.547388e-02  4.633778e-02  1.609726e-02  1.972850e-02
## [41] -1.009657e-02  2.503001e-02  1.756579e-02  3.233622e-02  1.713765e-03
## [46] -1.420616e-02  3.695312e-02  1.969619e-02 -2.232228e-03  3.368516e-02
## [51]  1.280396e-02 -3.145833e-03 -1.410629e-02  1.378674e-03 -1.760648e-02
## [56] -5.069552e-03  5.795681e-02 -1.057987e-02 -1.891434e-02 -1.276414e-02
## [61]  9.708798e-03 -2.951765e-03  6.391530e-03 -3.115499e-02 -2.418299e-04
## [66] -7.018119e-02 -6.048069e-03  1.040363e-02 -6.523838e-03  2.965056e-03
## [71] -3.087729e-04  2.981469e-03 -1.303210e-02 -2.845609e-03  3.957229e-02
## [76] -2.286447e-02  3.174005e-02  1.853770e-03  2.251642e-02 -1.533584e-02
## [81]  7.970650e-03 -3.064595e-03 -6.078222e-03  1.914248e-02  4.260836e-02
## [86]  2.728963e-02  1.442898e-02 -2.729256e-02 -2.265772e-02  6.418666e-03
## [91]  1.171058e-02  2.214036e-03 -8.750515e-03 -2.215513e-03 -2.559791e-03
toplot_df <-  melt(results[,c("DATES", "JP_STRATEGY_TODAY","JP_FIRST_BOND","JP_SECOND_BOND")],"DATES")

my_title <- paste("Macro Sentiment ML over Ravenpack ESS metrics and taxonomy ",my_pair[1],"/",my_pair[2],sep="")
g<-ggplot(
  toplot_df,aes(
    x = DATES,y = value,group = variable,color = variable
  )
) +
  geom_line() +
  scale_x_date() +
  ggtitle(my_title) + xlab("Time") + ylab("Cumulated sentiment") +
  theme(title = element_text(size = 12, face = 'bold')) +
  theme(legend.position = c(0.2,0.8), legend.box = "vertical") +
  theme(legend.background = element_rect(fill = "gray90")) +
  theme(legend.key.size = unit(0.7, "cm"))
print(g)

# Equally weighted trading strategy plot

plot(results$STRATEGY_TODAY)

# Equally weighted trading strategy metrics

print("Outputing statistical results for our equally weighted strategy")
## [1] "Outputing statistical results for our equally weighted strategy"
WeightMatrix <- results[,c("DATES","US_WEIGHT","DE_WEIGHT","JP_WEIGHT","GB_WEIGHT")]
colnames(WeightMatrix) <- c("DATE","US_WEIGHT","DE_WEIGHT","JP_WEIGHT","GB_WEIGHT")
ReturnSerie <- results[,c("STRATEGY_RETURN")]
turnover <- RP_GetTurnOver(WeightMatrix,-1)
RP_ReturnStats(ReturnSerie,21,TRUE,WeightMatrix,0.05,F)
## Warning in if (is.na(WeightMatrix)) {: the condition has length > 1 and
## only the first element will be used
## *********************************************************
## **********       STRATEGY STATISTICS           **********
## *********************************************************
## *****                NO FEE                         *****
## ****                                                 ****
##   Annualized Return:      0.0630548682260927 
##   Annualized Volatility:  0.0424303001250806 
##   Information Ratio:     1.48608112693553 
##   Hit Ratio:             0.642105263157895 
##   W/L Ratio:             1.7461225136007 
##   Turnover:              0.62453950151062 
##   P-Value:               6.50100712989859e-05 
##   Max Drawdown:          -0.0269384258163821 
##   Drawdown Recovery:     1 
##   Break-Even Fee (bps):  42.1782914994663 
## ****                                                 ****
## *********************************************************
## *****                WITH FEE                       *****
## ****                                                 ****
##   Annualized Return:      0.0556023057061378 
##   Annualized Volatility:  0.0423251080959486 
##   Information Ratio:     1.31369553930236 
##   Hit Ratio:             0.610526315789474 
##   W/L Ratio:             1.74038976547072 
##   Turnover:              0.62453950151062 
##   P-Value:               0.000367703609286684 
##   Max Drawdown:          -0.0275668168187796 
##   Drawdown Recovery:     2 
## ****                                                 ****
## *********************************************************
## $STATS
##   Annualized_Return Annualized_Return_Fee Annualized_Volatility
## 1        0.06305487            0.05560231             0.0424303
##   Annualized_Volatility_Fee       IR   IR_Fee  TurnOver  HitRatio
## 1                0.04232511 1.486081 1.313696 0.6245395 0.6421053
##   HitRatio_Fee  WLRatio WLRatio_Fee         PVal     PVal_Fee BreakEvenFee
## 1    0.6105263 1.746123     1.74039 6.501007e-05 0.0003677036     42.17829
##   maxDrawdown drawdownRecovery maxDrawdown_Fee drawdownRecovery_Fee
## 1 -0.02693843                1     -0.02756682                    2
## 
## $RETURN
##  [1]  0.0224817927  0.0073554431  0.0094109453 -0.0018065855 -0.0067892329
##  [6]  0.0175991635  0.0063767887 -0.0084109902  0.0211697519 -0.0003253825
## [11]  0.0191182697  0.0151417066  0.0034805435  0.0068032928 -0.0005487128
## [16] -0.0030360466  0.0131587823 -0.0194108518 -0.0076765830  0.0287629187
## [21]  0.0067439001  0.0073185296  0.0052877112  0.0150021504  0.0049365343
## [26] -0.0084065875 -0.0017678093  0.0039670399  0.0163433599 -0.0049275027
## [31]  0.0030812729  0.0352529180 -0.0051602099 -0.0010365297 -0.0144145396
## [36]  0.0007070710  0.0250527928  0.0207730364  0.0072841153  0.0038716122
## [41] -0.0035195263  0.0112551989  0.0191847562  0.0189120803 -0.0017974811
## [46] -0.0076957149  0.0246756704  0.0230488932  0.0024706016  0.0348764025
## [51]  0.0187123260  0.0060361622 -0.0079876147  0.0016755317 -0.0048647051
## [56]  0.0061731865  0.0247372873  0.0017106275 -0.0023798000  0.0067497283
## [61]  0.0101217418 -0.0032935528  0.0042765447 -0.0116148392  0.0068345352
## [66] -0.0154691087  0.0081471097  0.0147967374 -0.0010110040  0.0005376510
## [71] -0.0001056895 -0.0040035051  0.0052397422 -0.0012554881  0.0132671934
## [76] -0.0205404319  0.0295900835  0.0004342603  0.0275872789 -0.0054473758
## [81]  0.0082178271 -0.0055668265  0.0045578200  0.0067748355  0.0210134364
## [86] -0.0030823704  0.0039113572  0.0104700020 -0.0255458034  0.0064645537
## [91]  0.0107241457  0.0129925480 -0.0165386346  0.0005818353 -0.0086197247
## 
## $RETURN_FEE
##  [1]  2.199279e-02  6.492229e-03  8.791839e-03 -2.254886e-03 -7.223888e-03
##  [6]  1.670168e-02  5.800309e-03 -8.812604e-03  2.046923e-02 -9.977021e-04
## [11]  1.873528e-02  1.465405e-02  3.344668e-03  5.857505e-03 -1.464956e-03
## [16] -3.844125e-03  1.232267e-02 -1.947399e-02 -8.253561e-03  2.783465e-02
## [21]  6.381275e-03  7.036042e-03  4.585147e-03  1.439443e-02  4.244781e-03
## [26] -8.886511e-03 -2.440337e-03  3.634424e-03  1.586709e-02 -5.259369e-03
## [31]  2.590668e-03  3.442811e-02 -5.590623e-03 -1.692063e-03 -1.508844e-02
## [36]  2.643176e-04  2.425080e-02  1.979312e-02  6.540245e-03  3.483912e-03
## [41] -4.500896e-03  1.026590e-02  1.825417e-02  1.851687e-02 -2.692459e-03
## [46] -7.953867e-03  2.405674e-02  2.270597e-02  1.728217e-03  3.410014e-02
## [51]  1.802205e-02  5.452125e-03 -8.399378e-03  7.221300e-04 -5.831203e-03
## [56]  5.431845e-03  2.416167e-02  9.541924e-04 -2.884495e-03  6.065017e-03
## [61]  9.131322e-03 -3.860394e-03  3.765748e-03 -1.254035e-02  6.285880e-03
## [66] -1.629240e-02  7.679639e-03  1.417287e-02 -1.392909e-03 -9.405704e-05
## [71] -5.996093e-04 -4.523734e-03  4.570839e-03 -1.946036e-03  1.253625e-02
## [76] -2.120478e-02  2.931487e-02 -5.851029e-05  2.689510e-02 -5.822844e-03
## [81]  7.225519e-03 -6.388624e-03  3.561872e-03  5.967984e-03  2.060436e-02
## [86] -3.749597e-03  3.420155e-03  9.794112e-03 -2.611290e-02  6.092478e-03
## [91]  1.029131e-02  1.265664e-02 -1.659409e-02 -3.868202e-04 -9.597339e-03
toplot_df <-  melt(results[,c("DATES", "STRATEGY_TODAY","EQ_WEIGHTS_BOND")],"DATES")
my_title <- paste("Macro Sentiment ML over Ravenpack ESS metrics and taxonomy ",my_pair[1],"/",my_pair[2],sep="")
g<-ggplot(
  toplot_df,aes(
    x = DATES,y = value,group = variable,color = variable
  )
) +
  geom_line() +
  scale_x_date() +
  ggtitle(my_title) + xlab("Time") + ylab("Cumulated sentiment") +
  theme(title = element_text(size = 12, face = 'bold')) +
  theme(legend.position = c(0.2,0.8), legend.box = "vertical") +
  theme(legend.background = element_rect(fill = "gray90")) +
  theme(legend.key.size = unit(0.7, "cm"))
print(g)

# ExportPlot(g,outputDataPathStrategyMonth,paste(my_result_spread_name,"_bench",sep=""))

toplot_df <-  melt(results[,c("DATES", "STRATEGY_TODAY","JP_FIRST_BOND","JP_SECOND_BOND","DE_SECOND_BOND","GB_SECOND_BOND")],"DATES")
my_title <- paste("Macro Sentiment ML over Ravenpack ESS metrics and taxonomy ",my_pair[1],"/",my_pair[2],sep="")
g<-ggplot(
  toplot_df,aes(
    x = DATES,y = value,group = variable,color = variable
  )
) +
  geom_line() +
  scale_x_date() +
  ggtitle(my_title) + xlab("Time") + ylab("Cumulated sentiment") +
  theme(title = element_text(size = 12, face = 'bold')) +
  theme(legend.position = c(0.2,0.8), legend.box = "vertical") +
  theme(legend.background = element_rect(fill = "gray90")) +
  theme(legend.key.size = unit(0.7, "cm"))
print(g)

weights_decomposition_df <- data.frame(weights=results$US_WEIGHT,typo="US")
weights_decomposition_df <- rbind(weights_decomposition_df, data.frame(weights=results$JP_WEIGHT,typo="JP"))
weights_decomposition_df <- rbind(weights_decomposition_df, data.frame(weights=results$GB_WEIGHT,typo="GB"))
weights_decomposition_df <- rbind(weights_decomposition_df, data.frame(weights=results$DE_WEIGHT,typo="DE"))
my_title <-"Distribution of weights per country"
my_xaxis_title <- paste("Weights")
my_yaxis_title <- paste("Percentage","\n")

g <- ggplot(weights_decomposition_df, aes(x=weights, fill=typo)) +geom_density(alpha=.3)+
  scale_y_continuous(labels = percent_format())+scale_x_continuous(limits=c(-2,2))+facet_wrap(~typo, ncol = 2)
g <- g +ylab(my_yaxis_title)+xlab(my_xaxis_title)+
  theme(axis.text.x = element_text(size=12),axis.text.y = element_text(size=12),title =element_text(size=18, face='bold'))+
  theme(legend.position=c(0.9,0.85), legend.box = "vertical")+
  theme(legend.background = element_rect(fill="gray90"))+
  theme(legend.key.size = unit(1., "cm"))+
  theme(legend.text =  element_text(size=12,colour="black"))+
  theme(legend.title =  element_text(size=12,colour="black"))
print(g)

g <- ggplot(weights_decomposition_df, aes(x=weights, fill=typo)) +geom_density(alpha=.3)+
  scale_y_continuous(labels = percent_format())+scale_x_continuous(limits=c(-2,2))
g <- g +ylab(my_yaxis_title)+xlab(my_xaxis_title)+
  theme(axis.text.x = element_text(size=12),axis.text.y = element_text(size=12),title =element_text(size=18, face='bold'))+
  theme(legend.position=c(0.9,0.85), legend.box = "vertical")+
  theme(legend.background = element_rect(fill="gray90"))+
  theme(legend.key.size = unit(1., "cm"))+
  theme(legend.text =  element_text(size=12,colour="black"))+
  theme(legend.title =  element_text(size=12,colour="black"))
print(g)

toplot_df <-  melt(results[,c("DATES","US_WEIGHT","DE_WEIGHT","JP_WEIGHT","GB_WEIGHT")],"DATES")

my_title <-paste("Macro Sentiment ML over Ravenpack ESS metrics and taxonomy ",my_pair[1],"/",my_pair[2],sep="")
g<-ggplot(
  toplot_df,aes(
    x = DATES,y = value,group = variable,color = variable
  )
) +
  geom_line() +
  scale_x_date() +
  ggtitle(my_title) + xlab("Time") + ylab("Cumulated sentiment") +
  theme(title = element_text(size = 12, face = 'bold')) +
  theme(legend.position = c(0.2,0.8), legend.box = "vertical") +
  theme(legend.background = element_rect(fill = "gray90")) #+
  # theme(legend.key.size = unit(0.7, "cm"))
print(g)

# ExportPlot(g,outputDataPathStrategyMonth,paste(my_result_spread_name,"_weights",sep=""))
prediction_results <- readRDS(paste(outputDataPathStrategyMonth,"all_pairs_prediction_results_month_2007.rds", sep = ""))

# correlation between prediction and returns for US
print("Correlation US unidirection prediction")
## [1] "Correlation US unidirection prediction"
print(cor(prediction_results$USPrediction,prediction_results$USEffective))
## [1] 0.1541642
for (my_quantile in c(0.1,1/5,1/3)){
  US_df <- prediction_results[,c("USPrediction","USEffective")]
  US_df$my_tiles <- with(US_df, cut(USPrediction, 
                                    breaks=quantile(USPrediction, probs=seq(0,1, by=my_quantile)), 
                                    include.lowest=TRUE))
  
  my_IR_computation <- function(x){
    IR <- mean(x,na.rm=TRUE)/sd(x,na.rm=TRUE)*sqrt(12)
    return(IR) 
  }
  
  agg_US_df <- aggregate(US_df$USEffective, by=list(US_df$my_tiles),FUN=my_IR_computation)
  colnames(agg_US_df) <- c("Pred_quantile","Mean_Ret")
  ## Histogram plotting of the return average per prediction quantile
  g <- ggplot(agg_US_df, aes(x=Pred_quantile,y=Mean_Ret)) +
    geom_histogram(stat = "identity")
  print(g)
}
## Warning: Stacking not well defined when ymin != 0

## Warning: Stacking not well defined when ymin != 0

## Warning: Stacking not well defined when ymin != 0

Predictionbullish <- prediction_results$USPrediction >= 0 
NextReturnbullish <- prediction_results$USEffective >= 0 

Predictionbearish <- prediction_results$USPrediction <= 0 
NextReturnbearish <- prediction_results$USEffective <= 0 

print("Long confusion matrix")
## [1] "Long confusion matrix"
print(confusionMatrix(Predictionbullish, NextReturnbullish))
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction FALSE TRUE
##      FALSE    28   20
##      TRUE     22   34
##                                           
##                Accuracy : 0.5962          
##                  95% CI : (0.4954, 0.6913)
##     No Information Rate : 0.5192          
##     P-Value [Acc > NIR] : 0.07013         
##                                           
##                   Kappa : 0.1899          
##  Mcnemar's Test P-Value : 0.87737         
##                                           
##             Sensitivity : 0.5600          
##             Specificity : 0.6296          
##          Pos Pred Value : 0.5833          
##          Neg Pred Value : 0.6071          
##              Prevalence : 0.4808          
##          Detection Rate : 0.2692          
##    Detection Prevalence : 0.4615          
##       Balanced Accuracy : 0.5948          
##                                           
##        'Positive' Class : FALSE           
## 
print("Short confusion matrix")
## [1] "Short confusion matrix"
print(confusionMatrix(Predictionbearish, NextReturnbearish))
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction FALSE TRUE
##      FALSE    34   22
##      TRUE     20   28
##                                           
##                Accuracy : 0.5962          
##                  95% CI : (0.4954, 0.6913)
##     No Information Rate : 0.5192          
##     P-Value [Acc > NIR] : 0.07013         
##                                           
##                   Kappa : 0.1899          
##  Mcnemar's Test P-Value : 0.87737         
##                                           
##             Sensitivity : 0.6296          
##             Specificity : 0.5600          
##          Pos Pred Value : 0.6071          
##          Neg Pred Value : 0.5833          
##              Prevalence : 0.5192          
##          Detection Rate : 0.3269          
##    Detection Prevalence : 0.5385          
##       Balanced Accuracy : 0.5948          
##                                           
##        'Positive' Class : FALSE           
## 
print("Correlation GB unidirection prediction")
## [1] "Correlation GB unidirection prediction"
print(cor(prediction_results$GBPrediction,prediction_results$GBEffective))
## [1] 0.2115828
for (my_quantile in c(0.1,1/5,1/3)){
  df <- prediction_results[,c("GBPrediction","GBEffective")]
  df$my_tiles <- with(df, cut(GBPrediction, 
                              breaks=quantile(GBPrediction, probs=seq(0,1, by=my_quantile)), 
                              include.lowest=TRUE))
  agg_df <- aggregate(df$GBEffective, by=list(df$my_tiles),FUN=my_IR_computation)
  colnames(agg_df) <- c("Pred_quantile","Mean_Ret")
  ## Histogram plotting of the return average per prediction quantile
  g <- ggplot(agg_df, aes(x=Pred_quantile,y=Mean_Ret)) +
    geom_histogram(stat = "identity")
  print(g)
}
## Warning: Stacking not well defined when ymin != 0

## Warning: Stacking not well defined when ymin != 0

## Warning: Stacking not well defined when ymin != 0

Predictionbullish <- prediction_results$GBPrediction >= 0 
NextReturnbullish <- prediction_results$GBEffective >= 0 

Predictionbearish <- prediction_results$GBPrediction <= 0 
NextReturnbearish <- prediction_results$GBEffective <= 0 

print("Long confusion matrix")
## [1] "Long confusion matrix"
print(confusionMatrix(Predictionbullish, NextReturnbullish))
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction FALSE TRUE
##      FALSE    24   19
##      TRUE     21   40
##                                           
##                Accuracy : 0.6154          
##                  95% CI : (0.5149, 0.7091)
##     No Information Rate : 0.5673          
##     P-Value [Acc > NIR] : 0.1869          
##                                           
##                   Kappa : 0.2124          
##  Mcnemar's Test P-Value : 0.8744          
##                                           
##             Sensitivity : 0.5333          
##             Specificity : 0.6780          
##          Pos Pred Value : 0.5581          
##          Neg Pred Value : 0.6557          
##              Prevalence : 0.4327          
##          Detection Rate : 0.2308          
##    Detection Prevalence : 0.4135          
##       Balanced Accuracy : 0.6056          
##                                           
##        'Positive' Class : FALSE           
## 
print("Short confusion matrix")
## [1] "Short confusion matrix"
print(confusionMatrix(Predictionbearish, NextReturnbearish))
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction FALSE TRUE
##      FALSE    40   21
##      TRUE     19   24
##                                           
##                Accuracy : 0.6154          
##                  95% CI : (0.5149, 0.7091)
##     No Information Rate : 0.5673          
##     P-Value [Acc > NIR] : 0.1869          
##                                           
##                   Kappa : 0.2124          
##  Mcnemar's Test P-Value : 0.8744          
##                                           
##             Sensitivity : 0.6780          
##             Specificity : 0.5333          
##          Pos Pred Value : 0.6557          
##          Neg Pred Value : 0.5581          
##              Prevalence : 0.5673          
##          Detection Rate : 0.3846          
##    Detection Prevalence : 0.5865          
##       Balanced Accuracy : 0.6056          
##                                           
##        'Positive' Class : FALSE           
## 
print("Correlation DE unidirection prediction")
## [1] "Correlation DE unidirection prediction"
print(cor(prediction_results$DEPrediction,prediction_results$DEEffective))
## [1] 0.1146792
for (my_quantile in c(0.1,1/5,1/3)){
  df <- prediction_results[,c("DEPrediction","DEEffective")]
  df$my_tiles <- with(df, cut(DEPrediction, 
                              breaks=quantile(DEPrediction, probs=seq(0,1, by=my_quantile)), 
                              include.lowest=TRUE))
  agg_df <- aggregate(df$DEEffective, by=list(df$my_tiles),FUN=my_IR_computation)
  colnames(agg_df) <- c("Pred_quantile","Mean_Ret")
  ## Histogram plotting of the return average per prediction quantile
  g <- ggplot(agg_df, aes(x=Pred_quantile,y=Mean_Ret)) +
    geom_histogram(stat = "identity")
  print(g)
}
## Warning: Stacking not well defined when ymin != 0

## Warning: Stacking not well defined when ymin != 0

## Warning: Stacking not well defined when ymin != 0

Predictionbullish <- prediction_results$DEPrediction >= 0 
NextReturnbullish <- prediction_results$DEEffective >= 0 

Predictionbearish <- prediction_results$DEPrediction <= 0 
NextReturnbearish <- prediction_results$DEEffective <= 0 

print("Long confusion matrix")
## [1] "Long confusion matrix"
print(confusionMatrix(Predictionbullish, NextReturnbullish))
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction FALSE TRUE
##      FALSE    21   28
##      TRUE     23   32
##                                          
##                Accuracy : 0.5096         
##                  95% CI : (0.4097, 0.609)
##     No Information Rate : 0.5769         
##     P-Value [Acc > NIR] : 0.9311         
##                                          
##                   Kappa : 0.0104         
##  Mcnemar's Test P-Value : 0.5754         
##                                          
##             Sensitivity : 0.4773         
##             Specificity : 0.5333         
##          Pos Pred Value : 0.4286         
##          Neg Pred Value : 0.5818         
##              Prevalence : 0.4231         
##          Detection Rate : 0.2019         
##    Detection Prevalence : 0.4712         
##       Balanced Accuracy : 0.5053         
##                                          
##        'Positive' Class : FALSE          
## 
print("Short confusion matrix")
## [1] "Short confusion matrix"
print(confusionMatrix(Predictionbearish, NextReturnbearish))
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction FALSE TRUE
##      FALSE    32   23
##      TRUE     28   21
##                                          
##                Accuracy : 0.5096         
##                  95% CI : (0.4097, 0.609)
##     No Information Rate : 0.5769         
##     P-Value [Acc > NIR] : 0.9311         
##                                          
##                   Kappa : 0.0104         
##  Mcnemar's Test P-Value : 0.5754         
##                                          
##             Sensitivity : 0.5333         
##             Specificity : 0.4773         
##          Pos Pred Value : 0.5818         
##          Neg Pred Value : 0.4286         
##              Prevalence : 0.5769         
##          Detection Rate : 0.3077         
##    Detection Prevalence : 0.5288         
##       Balanced Accuracy : 0.5053         
##                                          
##        'Positive' Class : FALSE          
## 
print("Correlation GB unidirection prediction")
## [1] "Correlation GB unidirection prediction"
print(cor(prediction_results$GBPrediction,prediction_results$GBEffective))
## [1] 0.2115828
Predictionbullish <- prediction_results$GBPrediction >= 0 
NextReturnbullish <- prediction_results$GBEffective >= 0 

Predictionbearish <- prediction_results$GBPrediction <= 0 
NextReturnbearish <- prediction_results$GBEffective <= 0 

print("Long confusion matrix")
## [1] "Long confusion matrix"
print(confusionMatrix(Predictionbullish, NextReturnbullish))
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction FALSE TRUE
##      FALSE    24   19
##      TRUE     21   40
##                                           
##                Accuracy : 0.6154          
##                  95% CI : (0.5149, 0.7091)
##     No Information Rate : 0.5673          
##     P-Value [Acc > NIR] : 0.1869          
##                                           
##                   Kappa : 0.2124          
##  Mcnemar's Test P-Value : 0.8744          
##                                           
##             Sensitivity : 0.5333          
##             Specificity : 0.6780          
##          Pos Pred Value : 0.5581          
##          Neg Pred Value : 0.6557          
##              Prevalence : 0.4327          
##          Detection Rate : 0.2308          
##    Detection Prevalence : 0.4135          
##       Balanced Accuracy : 0.6056          
##                                           
##        'Positive' Class : FALSE           
## 
print("Short confusion matrix")
## [1] "Short confusion matrix"
print(confusionMatrix(Predictionbearish, NextReturnbearish))
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction FALSE TRUE
##      FALSE    40   21
##      TRUE     19   24
##                                           
##                Accuracy : 0.6154          
##                  95% CI : (0.5149, 0.7091)
##     No Information Rate : 0.5673          
##     P-Value [Acc > NIR] : 0.1869          
##                                           
##                   Kappa : 0.2124          
##  Mcnemar's Test P-Value : 0.8744          
##                                           
##             Sensitivity : 0.6780          
##             Specificity : 0.5333          
##          Pos Pred Value : 0.6557          
##          Neg Pred Value : 0.5581          
##              Prevalence : 0.5673          
##          Detection Rate : 0.3846          
##    Detection Prevalence : 0.5865          
##       Balanced Accuracy : 0.6056          
##                                           
##        'Positive' Class : FALSE           
## 
print("Correlation JP unidirection prediction")
## [1] "Correlation JP unidirection prediction"
print(cor(prediction_results$JPPrediction,prediction_results$JPEffective))
## [1] 0.1272445
for (my_quantile in c(0.1,1/5,1/3)){
  df <- prediction_results[,c("JPPrediction","JPEffective")]
  df$my_tiles <- with(df, cut(JPPrediction, 
                              breaks=quantile(JPPrediction, probs=seq(0,1, by=my_quantile)), 
                              include.lowest=TRUE))
  agg_df <- aggregate(df$JPEffective, by=list(df$my_tiles),FUN=my_IR_computation)
  colnames(agg_df) <- c("Pred_quantile","Mean_Ret")
  ## Histogram plotting of the return average per prediction quantile
  g <- ggplot(agg_df, aes(x=Pred_quantile,y=Mean_Ret)) +
    geom_histogram(stat = "identity")
  print(g)
}
## Warning: Stacking not well defined when ymin != 0

## Warning: Stacking not well defined when ymin != 0

## Warning: Stacking not well defined when ymin != 0

Predictionbullish <- prediction_results$JPPrediction >= 0 
NextReturnbullish <- prediction_results$JPEffective >= 0 

Predictionbearish <- prediction_results$JPPrediction <= 0 
NextReturnbearish <- prediction_results$JPEffective <= 0 

print("Long confusion matrix")
## [1] "Long confusion matrix"
print(confusionMatrix(Predictionbullish, NextReturnbullish))
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction FALSE TRUE
##      FALSE    27   25
##      TRUE     20   32
##                                           
##                Accuracy : 0.5673          
##                  95% CI : (0.4665, 0.6641)
##     No Information Rate : 0.5481          
##     P-Value [Acc > NIR] : 0.385           
##                                           
##                   Kappa : 0.1346          
##  Mcnemar's Test P-Value : 0.551           
##                                           
##             Sensitivity : 0.5745          
##             Specificity : 0.5614          
##          Pos Pred Value : 0.5192          
##          Neg Pred Value : 0.6154          
##              Prevalence : 0.4519          
##          Detection Rate : 0.2596          
##    Detection Prevalence : 0.5000          
##       Balanced Accuracy : 0.5679          
##                                           
##        'Positive' Class : FALSE           
## 
print("Short confusion matrix")
## [1] "Short confusion matrix"
print(confusionMatrix(Predictionbearish, NextReturnbearish))
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction FALSE TRUE
##      FALSE    32   20
##      TRUE     24   28
##                                           
##                Accuracy : 0.5769          
##                  95% CI : (0.4761, 0.6732)
##     No Information Rate : 0.5385          
##     P-Value [Acc > NIR] : 0.2461          
##                                           
##                   Kappa : 0.1538          
##  Mcnemar's Test P-Value : 0.6511          
##                                           
##             Sensitivity : 0.5714          
##             Specificity : 0.5833          
##          Pos Pred Value : 0.6154          
##          Neg Pred Value : 0.5385          
##              Prevalence : 0.5385          
##          Detection Rate : 0.3077          
##    Detection Prevalence : 0.5000          
##       Balanced Accuracy : 0.5774          
##                                           
##        'Positive' Class : FALSE           
## 

Variable importance visualization over time

#### All heat map variable importance viewing
#### results
my_pairs = list(c("US","DE"),c("US","GB"),c("US","JP"))
my_threshold <- 100
for (my_pair in my_pairs){
  
  # SaveDataFrame(my_total_df,outputDataPathMonth,paste(my_pair[1], my_pair[2],"variable_importance_spread_results_month_2007",sep=""))
  filename <- paste(my_pair[1], my_pair[2],"variable_importance_spread_results_month_2007.rds",sep="")
  my_total_df <- readRDS(paste(outputDataPathStrategyMonth,filename, sep = ""))
  print(dim(my_total_df))
  # we only keep the first 100 most important predictors by importance count over time
  # my_filtered_df <- my_total_df[(sort(rowSums(my_total_df[,-1]), index.return=TRUE, decreasing = TRUE))$ix<=my_threshold,] 
  my_index <- rowSums(my_total_df[,-1])!=0
  my_total_df <- my_total_df[my_index,]
  my_filtered_df <- my_total_df[(sort(rowSums(my_total_df[,-1]!=0), index.return=TRUE, decreasing = TRUE))$ix<=my_threshold,] 
  
  my_filtered_df$Feature <- as.factor(my_filtered_df$Feature) 
  my_filtered_df <- transform(my_filtered_df, Feature=reorder(Feature,(sort(rowSums(my_filtered_df[,-1]!=0), index.return=TRUE, decreasing = TRUE))$ix))
  my_filtered_df <- transform(my_filtered_df, Feature=reorder(Feature,rowSums(my_filtered_df[,-1])))
  
  
  my_filtered_df.m <- melt(my_filtered_df,id="Feature")
  colnames(my_filtered_df.m) <- c("Feature","Dates","Weight")
  # my_filtered_df.m <- ddply(my_filtered_df.m, .(variable), transform)
  # my_filtered_df.m <- ddply(my_filtered_df.m, .(variable), transform, rescale = rescale(value))
  p <- ggplot(my_filtered_df.m, aes(Dates, Feature)) + geom_tile(aes(fill = Weight),colour = "white") +
    scale_fill_gradient(low = "white", high = "steelblue")
  print(p)
  # ExportPlot(p,outputDataPathStrategyMonth,paste0(my_pair[2],"heatmap"), width=10, height=15)
  print("Done")
}
## [1] 263 105

## [1] "Done"
## [1] 263 105

## [1] "Done"
## [1] 142 105

## [1] "Done"